package com.example.rentingapp.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.rentingapp.R;
import com.example.rentingapp.api.Api;
import com.example.rentingapp.api.ApiConfig;
import com.example.rentingapp.api.TtitCallback;
import com.example.rentingapp.enetity.InformationResponse;
import com.example.rentingapp.enetity.LoginResponse;
import com.example.rentingapp.util.StringUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;

public class RegisterActivity extends BaseActivity implements View.OnClickListener {
    private Button btnRegister;
    private TextView tvHaveAccount;
    private EditText etAccount, etPassword;
    private String account, password;

    @Override
    protected int initLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        btnRegister = findViewById(R.id.btn_register);
        tvHaveAccount = findViewById(R.id.tv_have_account);
        etAccount = findViewById(R.id.et_register_account);
        etPassword = findViewById(R.id.et_register_password);
    }

    @Override
    protected void initData() {
        btnRegister.setOnClickListener(this);
        tvHaveAccount.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_register:
                account = etAccount.getText().toString().trim();
                password = etPassword.getText().toString().trim();
                register(account, password);
                break;
            case R.id.tv_have_account:
                navigateTo(LoginActivity.class);
                finish();
                break;
        }
    }

    private void register(String account, String password) {
        if (StringUtils.isEmpty(account)) {
            showToast("请输入账号");
            return;
        }
        if (StringUtils.isEmpty(password)) {
            showToast("请输入密码");
            return;
        }
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("account", account);
        params.put("password", password);
        Api.config(ApiConfig.REGISTER, params).postRequest(mContext, new TtitCallback() {
            @Override
            public void onSuccess(final String res) {
                Gson gson = new Gson();
                LoginResponse registerResponse = gson.fromJson(res, LoginResponse.class);
                if (registerResponse.getSuccess() == true) {
                    navigateTo(NewInformationActivity.class);
                } else {
                    showToastSync("该用户已存在");
                }
                if (registerResponse.getDatas() != null) {
                    String user_id_register = String.valueOf(registerResponse.getDatas().get(0).getUserId());
                    insertVal("user_id_register", user_id_register);//将user_id_register保存到本地
                }
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
    }
}